package com.csdj.common.utils;

import org.slf4j.LoggerFactory;

import java.util.*;

public class MessageUtil {



    //用于保存各种语言的信息
    private static Map<Locale, ResourceBundle> bundles = new HashMap<>();

    /**
     * 根据语言查询错误代码对应的错误描述
     * @param code 错误代码
     * @param lang 语言
     * @return code对应的消息
     */
    public static String getMsg(int code,Locale lang){
        if(lang == null){
            lang = Locale.CHINA;
        }
        ResourceBundle bundle = bundles.get(lang);
        if(bundle == null){
            //加载properties消息文件
            try{
                bundle = ResourceBundle.getBundle("message", lang);
                if(bundle != null){
                    bundles.put(lang,bundle);
                }else{
                    return "";
                }
            }catch (MissingResourceException e){
                LoggerFactory.getLogger("aop").error("没有message.properties文件");
                return "";
            }
        }
        return bundle.getString(String.valueOf(code));
    }

    public static String getMsg(int code){
        return getMsg(code,null);
    }
}
